package edu.washington.cs.workflow.matching;

import com.ibm.wala.ipa.callgraph.CGNode;

import edu.washington.cs.workflow.analysis.AnalysisBuilder;
import edu.washington.cs.workflow.analysis.AnalysisBuilder.CG;
import edu.washington.cs.workflow.analysis.AnalysisBuilder.UIFramework;
import edu.washington.cs.workflow.util.Utils;
import junit.framework.TestCase;

//1 OK
public class TestMatchingJEdit extends TestCase {

	public void testFindBuffers() {
		String[] pkgs = new String[]{"org.gjt.sp."};
		String oldDir = "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\jedit-2.5.jar";
		String oldMain = "Lorg/gjt/sp/jedit/jEdit";
		AnalysisBuilder oldBuilder = new AnalysisBuilder(oldDir, oldMain, pkgs);
		oldBuilder.setCGType(CG.RTA);
		oldBuilder.setUIFramework(UIFramework.SWING);
		oldBuilder.buildAnalysis();
		
		String newDir = "D:\\research\\configurations\\workspace\\workflow-error-repair\\experiments\\jedit-2.6.jar";
		String newMain = "Lorg/gjt/sp/jedit/jEdit";
		AnalysisBuilder newBuilder = new AnalysisBuilder(newDir, newMain, pkgs);
		newBuilder.setCGType(CG.RTA); //cannot use other CG algorithm
		newBuilder.setUIFramework(UIFramework.SWING);
		newBuilder.buildAnalysis();
		
		String sig1 = "org.gjt.sp.jedit.EditAction$Wrapper.actionPerformed(Ljava/awt/event/ActionEvent;)V";
		String sig2 = "org.gjt.sp.jedit.actions.prev_buffer.actionPerformed(Ljava/awt/event/ActionEvent;)V";
		String sig3 = "org.gjt.sp.jedit.actions.next_buffer.actionPerformed(Ljava/awt/event/ActionEvent;)V";
		
		CGNode node1 = oldBuilder.findCGNodeBySignature(sig1);
		System.out.println(node1);
		Utils.checkNotNull(node1);
		
		CGNode node2 = oldBuilder.findCGNodeBySignature(sig2);
		System.out.println(node2);
		Utils.checkNotNull(node2);
		
		CGNode node3 = oldBuilder.findCGNodeBySignature(sig3);
		System.out.println(node3);
		Utils.checkNotNull(node3);
		
		ExecutionTraceMatcher matcher = new ExecutionTraceMatcher(oldBuilder, newBuilder);
		
		CGNode matchedNode1 = matcher.findMatchedMethodByName(node1);
		System.out.println("Matched node 1: " + matchedNode1);
		Utils.checkNotNull(matchedNode1);
		CGNode counterpart1 = matcher.findCounterpart(node1);
		Utils.checkNotNull(counterpart1);
		
		CGNode matchedNode2 = matcher.findMatchedMethodByName(node2);
		System.out.println("Matched node 2: " + matchedNode2);
		Utils.checkNotNull(matchedNode2);
		CGNode counterpart2 = matcher.findCounterpart(node2);
		Utils.checkNotNull(counterpart2);
		
		CGNode matchedNode3 = matcher.findMatchedMethodByName(node3);
		System.out.println("Matched node 3: " + matchedNode3);
		Utils.checkNotNull(matchedNode3);
		CGNode counterpart3 = matcher.findCounterpart(node3);
		Utils.checkNotNull(counterpart3);
	}
	
}